package com.zj.core.view

import android.content.Context
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.util.AttributeSet
import android.widget.EditText
import com.fileutils.BaseConstant
import com.fileutils.BaseConstant.PATTERN_FETAL
import com.fileutils.BaseConstant.PATTERN_NURSE
import com.jniutils.DataCollUtil
import com.systemutils.SystemUtil
import com.zj.core.tools.Constant.Companion.PATTERN_FETAL_STRING

class RichEditText : EditText {
    private var mLevelType:String?=""

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    private var mStringBuilder: StringBuilder = StringBuilder()

    fun setTextValue(index:Int,charText: CharSequence) {
        var valueIndex = 0

        mStringBuilder.clear()
        mStringBuilder.append(text)
        mStringBuilder.insert(index,positionFoetus(charText.toString()))

        val nurseValue=searchNurseLevel(mStringBuilder.toString())
        mStringBuilder.clear().append(nurseValue)

        /**
         * 括号内容实现富文本
         */
        val spannableString = SpannableString(mStringBuilder)
        val indexList= SystemUtil.getGBKStrIndex(BaseConstant.PATTERN_IFBLACK,mStringBuilder.toString())
        for(bean in indexList){
            val value = bean.value
            valueIndex = mStringBuilder.indexOf(value, valueIndex)
            spannableString.setSpan(
                ForegroundColorSpan(Color.RED),
                valueIndex,
                valueIndex + value.length,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            )
            valueIndex += value.length
        }
        setText(spannableString)
    }

    /**
     * 查找等级护理相关字段
     */
    private var mNurseBuilder: StringBuilder = StringBuilder()
    private fun searchNurseLevel(mString: String): String {
        var startIndex = 0

        mNurseBuilder.clear()
        mNurseBuilder.append(mString)
        val nurseList=DataCollUtil.getProjectList(PATTERN_NURSE, mNurseBuilder.toString(),"")
        for(ObjBean in nurseList){
            val value = ObjBean.value
            //获取value的起始位置
            startIndex = mNurseBuilder.indexOf(value, startIndex)
            mNurseBuilder.delete(startIndex, startIndex + value.length)
            //将找到的字符串格式化后重新拼接到原始字符串的原始下标位
            mNurseBuilder.insert(startIndex, nurseLevelFormat(value))
            //下一起始位
            startIndex += value.length
        }
        return mNurseBuilder.toString()
    }

    /**
     * 实现等级护理级别转换
     */
    private fun nurseLevelFormat(nurseStr: String): String {
        when (mLevelType) {
            "0" -> {
                return romanNumable(nurseStr)
            }
            "1" -> {
                return meathLevel(nurseStr)
            }
            "2" -> {
                return charLevel(nurseStr)
            }
        }
        return nurseStr
    }

    fun setLevelType(levelType:String){
        this.mLevelType=levelType
    }

    /**
     * 胎方位转换
     */
    private fun positionFoetus(foetusStr: String):String{
        var startIndex = 0
        val fetalBuilder= StringBuilder(foetusStr)

        val nurseList=DataCollUtil.getProjectList(PATTERN_FETAL,foetusStr,PATTERN_FETAL_STRING)
        for(ObjBean in nurseList){
            val value = ObjBean.value
            //获取value的起始位置
            startIndex = fetalBuilder.indexOf(value, startIndex)
            fetalBuilder.delete(startIndex, startIndex + value.length)
            //将找到的字符串格式化后重新拼接到原始字符串的原始下标位
            fetalBuilder.insert(startIndex,formatFetal(value.replace(" ","")))
            //下一起始位
            startIndex += value.length
        }
        return fetalBuilder.toString()
    }

    /**
     * 规范处理胎方位
     */
    private fun formatFetal(itemStr:String):String{
        for(fetalItem in PATTERN_FETAL_STRING.split("|")){
            if(itemStr.toUpperCase()==fetalItem.toUpperCase()){
                return fetalItem
            }
        }
        return itemStr
    }

    /**
     * 护理等级罗马数字转换
     */
    private fun romanNumable(nurseStr: String): String {
        return nurseStr.replace("1", "Ⅰ")
            .replace("一", "Ⅰ").replace("2", "Ⅱ")
            .replace("二", "Ⅱ").replace("3", "Ⅲ")
            .replace("三", "Ⅲ").replace("4", "Ⅳ")
            .replace("四", "Ⅳ").replace("5", "Ⅴ")
            .replace("五", "Ⅴ").replace("6", "Ⅵ")
            .replace("六", "Ⅵ").replace("7", "Ⅶ")
            .replace("七", "Ⅶ").replace("8", "Ⅷ")
            .replace("八", "Ⅷ").replace("9", "Ⅸ")
            .replace("九", "Ⅸ").replace("10", "Ⅹ")
            .replace("十", "Ⅹ")
    }

    /**
     * 护理等级阿拉伯数字
     */
    private fun meathLevel(nurseStr: String): String {
        return nurseStr.replace("一", "1")
            .replace("二", "2")
            .replace("三", "3")
            .replace("四", "4")
            .replace("五", "5")
            .replace("六", "6")
            .replace("七", "7")
            .replace("八", "8")
            .replace("九", "9")
            .replace("十", "10")
    }

    /**
     * 护理等级中文数字
     */
    private fun charLevel(nurseStr: String): String {
        return nurseStr.replace("1", "一").replace("2", "二")
            .replace("3", "三").replace("4", "四")
            .replace("5", "五").replace("6", "六")
            .replace("7", "七").replace("8", "八")
            .replace("9", "九").replace("10", "十")
    }
}